<?php

namespace app\controller\main;

use linkphp\Application;
use linkphp\jwt\Jwt;

class Login
{

    public function __construct()
    {
        Application::singletonEager('login','app\\logic\\main\\Login');
    }

    public function login()
    {
        if(!Application::httpRequest()->isPost()){
            return ['code' => 2, 'msg' => '非法提交!'];
        }
        if(Application::input('post.account') == ''){
            return ['code' => 2, 'msg' => '账号不能为空!'];
        }
        if(Application::input('post.password') == ''){
            return ['code' => 2, 'msg' => '密码不能为空!'];
        }
        if($result = Application::get('login')->check()){
            return ['code' => 1, 'msg' => '登入成功!','token' => Jwt::sign(['user_id' => $result['id'],
                'username' => $result['user_name']],'123')];
        } else {
            return ['code' => 2, 'msg' => '登入失败!'];
        }
    }

}